from protoserv.server import ServiceSkeleton
from protoserv.server import MethodSkeleton

class CalcServiceSkeleton(ServiceSkeleton):
  def __init__(self, impl):
    methods = [
      MethodSkeleton(self, impl,
                     'protoserv.samples.CalcService.Calculate',
                     CalculateRequest.__class__,
                     CalculateResponse.__class__),
    ]
    CalcServiceSkeleton.__init__('protoserv.samples.CalcService', methods)


class CalcServiceSyncProxy(SyncProxy):
  def __init__(self, channel):
    SyncProxy.__init__(self, channel, 'protoserv.samples.CalcService')

  def Calculate(self, rpc, req):
    return self._invoke('Calculate', rpc, req)


class CalcServiceAsyncProxy(AsyncProxy):
  def __init__(self, channel):
    AsyncProxy.__init__(self, channel, 'protoserv.samples.CalcService')

  def Calculate(self, rpc, req, callback):
    self._invoke('Calculate', rpc, req, callback)


def main():
  rpc_server = RpcServer()
  rpc_server.register(CalcServiceSkeleton(CalcService()))
  rpc_server.bind(HttpEndpoint(8080))
  rpc_server.serve()

def main():
  channel = HttpChannel('http://localhost:8080/rpc')
  calc_sync = CalcServiceSyncProxy(channel)
  print calc_sync.Calculate(req)

def main():
  channel = HttpChannel('http://localhost:8080/rpc')
  calc_async = CalcServiceASyncProxy(channel)
  calc_async.Calculate(req, lambda rpc, res: print res)
